package com.example.sonone.service.impl;

import com.example.sonone.dao.CacheDao;
import com.example.sonone.pojo.SystemParam;
import com.example.sonone.pojo.SystemParamCron;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemParamCacheLoader {


    @Autowired
    private CacheDao cacheDao;

    /**
     * 初始化缓存数据
     */
    public void load() {
        List<SystemParam> paramList = cacheDao.getSysParam();
        Map<String, String> paramMap = new HashMap<String, String>();
        for (SystemParam systemParam : paramList) {
            paramMap.put(systemParam.getParamId(), systemParam.getParamValue());
        }
        SystemParamCache.getInstance().reload(paramMap);
    }

    /**
     * 初始化 cron 数据
     */
    public void loadCron() {
        List<SystemParamCron> paramList = cacheDao.getSysParamCron();
        Map<String, String> paramMap = new HashMap<String, String>();
        for (SystemParamCron systemParam : paramList) {
            paramMap.put(systemParam.getTaskName(), systemParam.getExpression());
        }
        SystemParamCache.getInstance().reloadCron(paramMap);
    }

}
